package com.lej.my.collect

import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.core.base.BaseFragment
import com.lej.core.common.Params.NFT_ID
import com.lej.core.common.Params.PACKAGE_ID
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.*
import com.lej.dataservice.club.bean.main.my.MyCollectInfo
import com.lej.my.BR
import com.lej.my.R
import com.lej.my.databinding.MyFragmentCollectBinding

/**
 *  author: cjq
 *  date  : 2022/6/9 0009
 *  desc  :  我的数字文创
 */
@Route(path = CRouter.myCollect)
class MyCollectFragment : BaseFragment<MyFragmentCollectBinding, MyCollectViewModel>() {
    lateinit var adapter: MyCollectAdapter
    override fun getLayout(): Int = R.layout.my_fragment_collect

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        super.initData()
        initAdapter()
    }

    private fun initAdapter() {
        adapter = MyCollectAdapter(R.layout.my_fragment_item_collect)
        binding.recyclerView.adapter = adapter
        adapter.loadMoreModule.setOnLoadMoreListener {
            requestData(true)
        }
    }

    override fun onResume() {
        super.onResume()
        requestData(false)
    }

    private fun requestData(isLoadMore: Boolean) {
        viewModel.getCollectData(isLoadMore = isLoadMore) { loadResult, loadMoreResult ->
            loadResult?.let { adapter.setList(it) }
            loadMoreResult?.let { adapter.addData(it) }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        adapter.setOnItemClickListener { adapter, _, position ->
            val info: MyCollectInfo = adapter.data[position] as MyCollectInfo
            if (info.isTrans()) {
                "该藏品处于”转移中“，${getTimeSpanDetail(info.getTime().toLong())}后允许转让/出售/兑换权益".toast()
            } else {
//                router(CRouter.nftDetail) {
//                    withInt(NFT_ID, info.nft_id ?: -1)
//                    withInt(PACKAGE_ID, info.id ?: -1)
//                }
            }
        }

        viewModel.loadingState.observe(this) {
            when (it) {
                LoadMoreSuccess -> {
                    adapter.loadMoreModule.loadMoreComplete()
                }
                LoadMoreError -> {
                    adapter.loadMoreModule.loadMoreFail()
                }
                LoadMoreComplete -> {
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
                LoadEmpty -> {
                    binding.flContainer.showPageState(PageState.Empty("啊哦 空空如也~"))
                }
                LoadError -> {
                    binding.flContainer.showPageState(PageState.Error("啊哦 网络异常～") { itt ->
                        itt.clickLaunch {
                            requestData(false)
                        }
                    })
                }
                LoadLoading -> {
                    binding.flContainer.showPageStateLoading()
                }
                LoadSuccess -> {
                    binding.flContainer.restorePageState()
                }
            }
        }
    }
}